﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace OpenRPOSBackOffice
{
    public partial class PeriodEditor : ChildWindow
    {
        DateTime startDate;
        int currentEmployeeID;

        public PeriodEditor()
        {
            InitializeComponent();
        }

        private async void btnOK_Click(object sender, RoutedEventArgs e)
        {
            this.IsEnabled = false;
            // Save the updated schedule information
            await sc0.save();
            await sc1.save();
            await sc2.save();
            await sc3.save();
            await sc4.save();
            await sc5.save();
            await sc6.save();
            Globals.schedule.refreshData(true);
            this.IsEnabled = true;
            this.DialogResult = true;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Globals.schedule.refreshData(true);
            this.DialogResult = false;
        }

        public void init(int employeeID, DateTime selectedDate)
        {
            this.IsEnabled = false;
            startDate = selectedDate;
            currentEmployeeID = employeeID;
            // Initialize the schedule control
            sc0.init(employeeID, selectedDate);
            sc1.init(employeeID, selectedDate.AddDays(1));
            sc2.init(employeeID, selectedDate.AddDays(2));
            sc3.init(employeeID, selectedDate.AddDays(3));
            sc4.init(employeeID, selectedDate.AddDays(4));
            sc5.init(employeeID, selectedDate.AddDays(5));
            sc6.init(employeeID, selectedDate.AddDays(6));
            // No preferences for managers...
            btnLoadPreferences.Visibility = (Globals.lstManagers.Contains(employeeID)||!Globals.currentLogin.col_Manager) ? Visibility.Collapsed : Visibility.Visible;
            cbClearSchedule.Visibility = btnLoadPreferences.Visibility;
            btnLoadTemplate.Visibility = btnLoadPreferences.Visibility;
            btnCopySchedule.Visibility = btnLoadPreferences.Visibility;
            cbClearSchedule.IsChecked = true;
            this.IsEnabled = true;
            this.Height = Globals.k_int_period_editor_height;
        }

        private void btnLoadPreferences_Click(object sender, RoutedEventArgs e)
        {
            this.IsEnabled = false;
            // Load employee preferences
            sc0.loadPreferences(cbClearSchedule.IsChecked == true);
            sc1.loadPreferences(cbClearSchedule.IsChecked == true);
            sc2.loadPreferences(cbClearSchedule.IsChecked == true);
            sc3.loadPreferences(cbClearSchedule.IsChecked == true);
            sc4.loadPreferences(cbClearSchedule.IsChecked == true);
            sc5.loadPreferences(cbClearSchedule.IsChecked == true);
            sc6.loadPreferences(cbClearSchedule.IsChecked == true);
            this.IsEnabled = true;
        }

        private async void btnLoadTemplate_Click(object sender, RoutedEventArgs e)
        {
            // Bring up the "Load Template" dialog...
            LoadScheduleTemplateDialog lstd = new LoadScheduleTemplateDialog();
            await lstd.loadScheduleTemplates(this, currentEmployeeID, startDate);
            lstd.Show();
        }

        private void btnCopySchedule_Click(object sender, RoutedEventArgs e)
        {
            // Bring up the "Copy Schedule" dialog...
            CopyScheduleDialog csd = new CopyScheduleDialog();
            csd.loadEmployees(this, currentEmployeeID, startDate);
            csd.Show();
        }
    }
}

